import { Component, ElementRef, ViewChild } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { UpgradeAppProvider } from "../../providers/upgrade-app/upgrade-app";
import { ApiProvider } from "../../providers/api/api";

/**
 * Generated class for the BarragePage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-barrage',
  templateUrl: 'barrage.html',
})
export class BarragePage {
  @ViewChild('togg')
  toggInput: ElementRef;
  courseclassid;
  unitId;
  teacherId;//教师ID
  barrshow = false;//弹幕开启状态
  barrlist = [];
  checked;
  n;
  lightsta = false;//弹幕开关开启
  lig;
  constructor(public element: ElementRef, public navCtrl: NavController, public navParams: NavParams, private wsService: UpgradeAppProvider, public http: ApiProvider) {
  }

  ionViewDidLoad() {

  }
  ionViewDidEnter() {
    this.courseclassid = this.navParams.get("courseclassid");
    this.unitId = this.navParams.get("unitId");
    this.teacherId = this.navParams.get("teacherId");

    //进入接收服务端消息
    this.http.post("/bullentTeacherController/getBullentTeacherList.do", { classcourseid: +this.courseclassid, unitId: this.unitId }).subscribe(item => {
      if (item["data"][0]) {
        this.barrlist = item["data"];
        this.barrlist.forEach(e => {
          e.sta = false;
        })
        console.log(123456)
        console.log(this.barrlist)
      }

    })
    console.log(69)
    console.log(this.courseclassid,this.unitId)
    this.http.post("/courseTeacherController/getStartCourseOfTeacher.do", { classcourseid: this.courseclassid, unitid: this.unitId }).subscribe(item => {
      console.log(12)
      console.log(item)


      //0开启
      if (item["bullentState"] == 0) {
        this.checked = true;
        this.barrshow = true;
        //弹幕开关开启接着判断
        this.lightsta = true;//判断哪个点亮还是点灭
        this.Lightselect();

      } else if (item["bullentState"] == 1) {
        this.checked = false;
        this.barrshow = false;
      }


    })

  }

  ngAfterViewInit() {
    //发送消息（不是发服务器）
    this.wsService.createObservableSocket(`${this.http.sendwsinfo}/FourInOneTeacherMgt/websocket`)//ws://192.168.0.28:8080/FourInOneTeacherMgt/websocket
      .subscribe(
        data => data,
        err => console.log(err),
        () => console.log('it s over')
      )
    //接收弹幕消息
    this.wsService.creatsocketbarr(`${this.http.receivews}/FourInOneAppServer/websocket`)//ws://192.168.0.28:8080/FourInOneAppServer/websocket
      .subscribe(
        data => this.data_stabarr(data),
        err => console.log(err),
        () => console.log('it s over')
      )

    //接收服务器消息
    this.wsService.creatsocket(`${this.http.receiveserve}/FourInOneTeacherAppServer/websocket`)//ws://192.168.0.35:8081/FourInOneTeacherAppServer/websocket
      .subscribe(
        data => this.data_staj(data),
        err => console.log(err),
        () => console.log('it s over')
      )
  }
  //进入时候弹幕开启时候判断哪个点亮
  Lightselect() {
    console.log(4578986)
    console.log(`getBrightBullent,${this.courseclassid},${this.unitId}`)
    this.wsService.sendMessage(`getBrightBullent,${this.courseclassid},${this.unitId}`);
  }
  //监控内容
  data_stabarr(a) {
    if (JSON.parse(a).Real_Name) {
      JSON.parse(a).sta = false;
      console.log(1111111)
      console.log(a)
      this.barrlist.unshift(JSON.parse(a));
      console.log(this.barrlist)
    }


  }
  stat;//监控点击还是收到
  stata;
  statald;//点亮
  stataup;//点灭
  stataldt;//点亮
  stataupt;//点灭
  ststan = 0;
  //监控点亮，点灭
  data_staj(a) {
    console.log(258369)
    console.log(a)
    if (a.split(',')[0] == "currentBrightBullentId") {
      if (a.split(',')[4] == -1) {
        this.barrlist.forEach((onerow, index) => {
          onerow.sta = false;
        })
      } else {

        this.barrlist.forEach((onerow, index) => {
          onerow.sta = false;
        })
        this.barrlist.forEach((onerow, i) => {
          if (onerow.id == a.split(',')[4]) {
            onerow.sta = true;
          }
        })
      }
    }
    if (a.split(',')[0] == "updateDM") {

      this.action(2);

    }

    if (a.split(',')[0] == "updateBullentState") {
      this.barrlist.forEach((onerow, index) => {
        onerow.sta = false;
      })

      this.barrlist.forEach((onerow, i) => {
        if (onerow.id == a.split(',')[4]) {
          onerow.sta = true;
        }
      })

      // this.Light(a, 2, 2)
      // if (this.ststan > 0) {
      // this.statald = 1;//点亮
      //   this.stataldt = 1;//点亮







      // if (this.ststa == 0) {
      //   this.Light(0, 0, 0);
      //   this.Light(0, 0, 0);
      // }
      // if(this.ststa == 1){
      //   this.Light(0, 0, 0);
      // }






      // this.Light_up.forEach((onerow, index) => {
      //   if(onerow){

      //   }
      // })
      // this.Light();
    }

    if (a.split(',')[0] == "DM_BullentState") {


      this.barrlist.forEach((onerow, index) => {
        onerow.sta = false;
      })


      // this.Light(a, 2, 3)
      // this.stataup = 1;//点灭
      // this.stataupt = 1;//点灭







    }
  }



  Light_up = [];
  Light_up1;
  num;//点亮标识

  //开启关闭弹幕
  action(a) {

    if (a == 2) {
      this.stata = 1;
      if (this.checked) {
        this.barrshow = false;
        this.checked = !this.checked;
        //开关关闭，点亮关闭
        this.barrlist.forEach((onerow, index) => {
          onerow.sta = false;
        })
      } else {

        this.checked = true;
        this.barrshow = true;

      }



    } else {
      if (this.stata == 1) {
        this.stata = 2;
      } else {
        if (a) {


          console.log(`9999`)
          console.log(`updateDM,${this.courseclassid},${this.unitId}`);

          this.barrlist.forEach((onerow, index) => {
            onerow.sta = false;
          })
          this.barrshow = true;
          this.wsService.sendMessage(`updateDM,${this.courseclassid},${this.unitId}`);
        } else {

          this.wsService.sendMessage(`updateDM,${this.courseclassid},${this.unitId}`);
          this.barrlist.forEach((onerow, index) => {
            onerow.sta = false;
          })


          this.barrshow = false;




        }

      }

    }



  }



  ni = true;
  texttogg = false;

  //点亮点灭
  Light(a, i, s) {
    if (s) {
      this.barrlist.forEach((onerow, index) => {
        onerow.sta = false;
      })
      this.wsService.sendMessage(`updateBullentState,${this.courseclassid},${this.unitId},${a}`);
    } else {
      this.barrlist.forEach((onerow, index) => {
        onerow.sta = false;
      })
      this.barrlist[i].sta = true;
      this.wsService.sendMessage(`updateBullentState,${this.courseclassid},${this.unitId},${a}`);


    }
    // let pd = this.toggInput.nativeElement;
    // console.log(78998)
    // console.log(pd)
    // if (pd.checked) {
    //   pd.checked = "true";
    // }else{
    //   console.log("没有选中");
    //   pd.checked = "false";
    // }








    // if (s == "check") {
    //   this.barrlist.forEach((onerow, index) => {
    //     onerow.sta = "check";
    //   })
    //   this.barrlist[i].sta = "checkbox";
    //   this.wsService.sendMessage(`updateBullentState,${this.courseclassid},${this.unitId},${a}`);
    // } else {
    //   this.wsService.sendMessage(`updateBullentState,${this.courseclassid},${this.unitId},${a}`);
    // }


    // if (s === 2) {//点亮
    //   this.barrlist.forEach((onerow, index) => {
    //     onerow.sta = false;
    //   })

    //   this.barrlist.forEach((onerow, i) => {
    //     if (onerow.id == a.split(',')[4]) {
    //       onerow.sta = true;
    //       this.n = i;

    //     }
    //   })

    // }
    // if (s === 3) {//点灭
    //   this.barrlist.forEach((onerow, index) => {
    //     onerow.sta = false;
    //   })

    // }

    // if (s == true) {
    //   alert(12)
    //   if (this.stataup == 1) {
    //     this.stataup == 2;
    //     return
    //   }
    //   alert(2)
    //   this.statald = 1;
    //   this.barrlist.forEach((onerow, index) => {
    //     onerow.sta = false;
    //   })
    //   this.barrlist[i].sta = true;

    //   this.wsService.sendMessage(`updateBullentState,${this.courseclassid},${this.unitId},${a}`);
    // } else {
    //   if (this.statald == 1) {
    //     this.statald == 2;
    //     return
    //   }
    //   alert(1)
    //   this.barrlist[i].sta = false;
    //   this.wsService.sendMessage(`updateBullentState,${this.courseclassid},${this.unitId},${a}`);
    // }


    //   if (s === 2) {
    //     alert(12)
    //     this.stataup = 1;
    //     this.statald = 1
    //     this.Light_up = [];
    //     this.barrlist.forEach((onerow, index) => {
    //       this.Light_up.push(false);

    //     })

    //     this.barrlist.forEach((onerow, i) => {
    //       if (onerow.id == a.split(',')[4]) {
    //         this.Light_up[i] = true;
    //         this.n = i;

    //       }
    //     })

    //   }else if(s === 3){
    //     alert(13)
    //     this.statald = 1
    //     this.Light_up = [];
    //     this.barrlist.forEach((onerow, index) => {
    //       this.Light_up.push(false);
    //     })
    //   } else if (s == true) {


    //     if(this.stataup == 1){
    //       this.stataup = 2;

    //     }else{
    //       alert(993)
    //       this.Light_up = [];
    //       this.Light_up.forEach((onerow, index) => {
    //         onerow=false;
    //       })
    //       this.Light_up[i] = true;
    //       this.wsService.sendMessage(`updateBullentState,${this.courseclassid},${this.unitId},${a}`);
    //     }

    //   } else if (s == false) {

    //     if(this.statald == 1){
    //       this.statald = 2;
    //     }else{
    //       this.Light_up = [];
    //       this.barrlist.forEach((onerow, index) => {
    //         this.Light_up.push(false);
    //       })
    //       this.wsService.sendMessage(`updateBullentState,${this.courseclassid},${this.unitId},${a}`);
    //     }

    //   }
    // alert(12)
    // // this.ststa = 1;
    // if (s == true) {
    //   alert(66)
    //   this.ni = false;
    //   localStorage.setItem('numll', i);
    //   // localStorage.setItem('numl', i);
    //   this.num = i;

    //   this.Light_up = [];

    //   this.barrlist.forEach((onerow, index) => {
    //     this.Light_up.push(false);

    //   })
    //   console.log(5555)
    //   console.log(this.Light_up)

    //   this.Light_up[i] = true;
    //   console.log(`updateBullentState`)

    //   console.log(`updateBullentState,${this.courseclassid},${this.unitId},${a}`)
    //   if (this.stataup == 1) {
    //     this.stataup = 2;
    //   }
    //   if (this.statald == 1) {
    //     this.statald = 2;

    //     return
    //   } else {
    //     this.wsService.sendMessage(`updateBullentState,${this.courseclassid},${this.unitId},${a}`);
    //   }



    // } else {
    //   alert(77)
    //   if (this.stataup == 1) {
    //     this.stataup = 2;
    //     alert(88)
    //     return
    //   } else {
    //     if (this.ni) {
    //       this.wsService.sendMessage(`updateBullentState,${this.courseclassid},${this.unitId},${a}`);
    //     } else {
    //       if (localStorage.getItem("numll") == i) {

    //         console.log(`DM_BullentState12`)
    //         console.log(`DM_BullentState,${this.courseclassid},${this.unitId},${a}`)
    //         this.wsService.sendMessage(`updateBullentState,${this.courseclassid},${this.unitId},${a}`);
    //       }
    //     }
    //   }




    // }











    // if(s == false&&this.lig==1){
    //   this.lig=2;
    //   this.wsService.sendMessage(`updateBullentState,${this.courseclassid},${this.unitId},${a}`);
    // }



  }




}
